<?php
include_once 'utiles.php';

function empresaSesion() {
	return $_SESSION['empresa'];
}

?>

<script type="text/javascript">
function ShowSelectedRows(form){

		var fechaD = document.getElementById('fechaDesde');
		var fechaH = document.getElementById('fechaHasta');
		var SelOperarios = "";
		var e = document.getElementById("estado_liq");
		var Liq = e.options[e.selectedIndex].value;

		if (fechaD.value == "" || fechaH.value == "") 
		{ 
			alert("Favor de ingresar un rango de fechas"); 
			return;
		} 

		for (x=0;x<form.operarios.length;x++)
		{
			if (form.operarios[x].selected)
			{
				if (SelOperarios) {
					SelOperarios = SelOperarios + ",'" + form.operarios[x].value + "'";
					}
				else {
					SelOperarios = "'" + form.operarios[x].value + "'";
					}
			}
		}

		if (SelOperarios == "") 
		{ 
			alert("Favor de ingresar al menos un operario"); 
			return;
		}

    // Create a new element input 
    var fd = document.createElement("input");
    var fh = document.createElement("input");
    var el = document.createElement("input");
    var es = document.createElement("input");
 
    // Add the new element to our form. 
    form.appendChild(fd);
    fd.name = "fd";
    fd.type = "hidden";
    fd.value = fechaD.value;
    
      // Add the new element to our form. 
    form.appendChild(fh);
    fh.name = "fh";
    fh.type = "hidden";
    fh.value = fechaH.value;  

      // Add the new element to our form. 
    form.appendChild(el);
    el.name = "el";
    el.type = "hidden";
    el.value = Liq;  

      // Add the new element to our form. 
    form.appendChild(es);
    es.name = "es";
    es.type = "hidden";
    es.value = SelOperarios;      
 
    // Make sure the plaintext password doesn't get sent. 
    fechaD.value = "";
 		fechaH.value = "";
 		Liq = "";
 		SelOperarios = "";
 		
    // Finally submit the form. 
    form.submit();

    // replace your own javascript here
    //alert(rows);
}
</script>

<?php
if(login_check() == true) {
	
// New Connection
$db = new mysqli(PHPGRID_DB_HOSTNAME, PHPGRID_DB_USERNAME, PHPGRID_DB_PASSWORD, PHPGRID_DB_NAME);

$result = $db->query("SELECT ID_OPERARIO, NOMBRE FROM serverinstall.operarios WHERE EMPRESA = ".empresaSesion());

if($result){

     // Cycle through results
    while ($row = $result->fetch_array()){
			$operarios[] = $row;
    }
    // Free result set
    $result->close();
}

$db->close();
	
?>

	<form class="form-signin" role="form" action="productividadOperarios.php" method="post" name="productividadOperarios">
	<div class="btn-group">
		<p>Fecha Desde: <input type="date" class="form-control" id="fechaDesde" display="dd/mm/yyyy" placeholder="Ingresar Fecha Desde"></p>
		<p>Fecha Hasta: <input type="date" class="form-control" id="fechaHasta" display="dd/mm/yyyy" placeholder="Ingresar Fecha Hasta"></p>
		<p>Estado Liquidacion: <select name="estado_liq" id="estado_liq" class="form-control">
			<option value="L">Liquidado</option>
			<option value="P">Pendiente</option>
			<option value="A">Ambos</option>
		</select></p>
		<p>Operarios: <select multiple name="operarios" id="operarios" class="form-control">
		<?php
		foreach ($operarios as $es) {
			  echo '<option value="'.$es['ID_OPERARIO'].'">'.$es['NOMBRE'].'</option>';
				}
		?>
		</select></p>
  	</br>
		</br>
	  <button type="button" class="btn btn-default"  onclick="ShowSelectedRows(this.form)">Reporte Productividad Operarios</button>
	 </div>
	</form>

<?php
} else {
	header('Location: login.php?error=2');
}	
?>

